import java.util.*;


/**
 * 染色问题
 * https://leetcode.cn/problems/flood-fill/
 */
class Solution {

    int[] dx = new int[]{0,0,1,-1};
    int[] dy = new int[]{1,-1,0,0};
    //BFS宽度优先遍历
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        //1.先判断修改的元素与原来演示说法相等
        if(image[sr][sc] == color) return image;
        int n = image.length;
        int m = image[0].length;
        int pre = image[sr][sc];

        //2.创建队列
        Queue<int[]> q = new LinkedList<>();
        q.offer(new int[]{sr,sc});

        while(!q.isEmpty()){
            int[] t = q.poll();
            int a = t[0];
            int b = t[1];
            image[a][b] = color;
            //上下左右遍历
            for(int i = 0;i < 4;i++){
                int x = dx[i] + a;
                int y = dy[i] + b;

                if(x >=0 && x < n && y >= 0 && y < m && image[x][y] == pre){
                    image[x][y] = color;
                    q.offer(new int[]{x,y});
                }
            }
        }
        return image;
    }
}
class Solution1 {
    int[] dx = new int[]{0,0,-1,1};
    int[] dy = new int[]{-1,1,0,0};
    boolean[][] vis;
    public int numIslands(char[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        vis = new boolean[n][m];
        int ret = 0;
        for(int i = 0;i < n;i++){
            for(int j = 0;j < m;j++){
                if(grid[i][j] == '1' && !vis[i][j]){
                    ret++;
                    bfs(grid,i,j,n,m);
                }
            }
        }
        return ret;
    }

    public void bfs(char[][] grid,int i,int j,int n,int m){

        vis[i][j] = true;

        for(int k = 0;k < 4;k++){
            int x = dx[k] + i;
            int y = dy[k] + j;
            if(x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && grid[x][y] == '1'){
                bfs(grid,x,y,n,m);
            }
        }
    }

    public static void main(String[] args) {
        Solution1 s = new Solution1();
        char[][] grid = new char[][]{
                {'1','1','1','1','0'},
                {'1','1','0','1','0'},
                {'1','1','0','0','0'},
                {'0','0','0','0','0'}
        };

        int ret = s.numIslands(grid);
        System.out.println(ret);
    }
}

/**
 * 多元BFS问题：01矩阵
 * https://leetcode.cn/problems/01-matrix/
 */
class Solution3 {
    public int[][] updateMatrix(int[][] mat) {
        //1.预处理
        int n = mat.length,m = mat[0].length;
        Queue<int[]> q = new LinkedList<>();
        //2.初始化
        int dist[][] = new int[n][m];
        for(int i = 0;i < n;i++)
            for(int j = 0;j < m;j++){
                if(mat[i][j] == 0){
                    dist[i][j] = 0;
                    q.offer(new int[]{i,j});
                }else{
                    dist[i][j] = -1;
                }
            }
        int[] dx = {0,0,-1,1};
        int[] dy = {-1,1,0,0};
        //3.开始多源BFS
        while(!q.isEmpty()){
            int[] t = q.poll();
            int a = t[0],b = t[1];

            for(int k = 0;k < 4;k++){
                int x = a + dx[k];
                int y = b + dy[k];

                if(x >= 0 && x < n && y >= 0 && y < m && mat[x][y] == 1 && dist[x][y] == -1){
                    dist[x][y] = dist[a][b] + 1;
                    q.offer(new int[]{x,y});
                }

            }
        }
        return dist;
    }
}

class Solution4
{
    int[] dx = {0,0,-1,1};
    int[] dy = {-1,1,0,0};
    public int nearestExit(char[][] maze, int[] entrance)
    {
        int n = maze.length,m = maze[0].length;

        boolean vis[][] = new boolean[n][m];

        Queue<int[]> q = new LinkedList<int[]>();
        q.offer(new int[]{entrance[0],entrance[1]});
        vis[entrance[0]][entrance[1]] = true;

        //统计步数
        int sept = 0;
        while(!q.isEmpty()) {
            sept++;//返回时对应扩展层数
            int size = q.size();
            for (int i = 0; i < size; i++) {
                int[] t = q.poll();
                int a = t[0], b = t[1];

                for (int k = 0; k < 4; k++) {
                    int x = dx[k] + a;
                    int y = dy[k] + b;
                    if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && maze[x][y] == ',') {
                        //判断一下是否为边界
                        if (x == 0 || y == 0 || x == n - 1 || y == m - 1) return sept;
                        q.offer(new int[]{x, y});
                        vis[x][y] = true;
                    }
                }
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        Solution4 s = new Solution4();
        char[][] maze ={{'+','+','.','+'},{'.','.','.','+'},{'+','+','+','.'}};
        s.nearestExit(maze,new int[]{1,2});
    }
}